﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZSZ.CommonMVC;
using ZSZ.DTO;
using ZSZ.IService;
using ZSZ.Model;
using ZSZ.Website.Areas.WebAdmin.Filters;

namespace ZSZ.Website.Areas.WebAdmin.Controllers
{
    public class PermissionController : Controller
    {
        public IPermissionService permissionService { get; set; }
        // GET: WebAdmin/Permission
        [AdminAuthorize(IsCheck = true)]
        public ActionResult List(int pageIndex = 1)
        {
            long total;
            int pageSize = 10, maxPagerCount = 10;
            List<Permission> list = permissionService.GetPagedData(out total, pageIndex, pageSize).ToList();
            ViewBag.totalCount = total;
            ViewBag.pageIndex = pageIndex;
            ViewBag.pageSize = pageSize;
            ViewBag.maxPagerCount = maxPagerCount;
            return View(list);
        }

        [HttpGet]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult Add()
        {
            ViewBag.SelectListHttpMethod = new SelectList(new string[] { "GET", "POST", "GET/POST" });
            return View();
        }

        [HttpPost]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult Add(string name,string description, string controllerName,string actionName,string httpMethod)
        {
            if (string.IsNullOrEmpty(name))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "权限名称不能为空" });
            }
            if (permissionService.Exist(name))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "权限名称已存在" });
            }
            if (string.IsNullOrEmpty(controllerName))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "控制器名称不能为空" });
            }
            if (string.IsNullOrEmpty(actionName))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "Action名称不能为空" });
            }
            if (string.IsNullOrEmpty(httpMethod))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "请选择请求方式" });
            }
            permissionService.Add(name,  description,  controllerName,  actionName,  httpMethod);
            return Json(new AjaxResult { Status = "ok" });
            //return View();
        }

        [HttpGet]
        public string Exist(string Name)
        {
            //remote:远程地址只能输出 "true" 或 "false"，不能有其他输出。
            //存在： "false"
            return permissionService.Exist(Name) ? "false":"true"  ;
        }

        [HttpGet]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult Edit(long id)
        {
            Permission model = permissionService.GetById(id);
            if (model == null)
            {
                return View("Error", (object)"获取不到权限信息");
            }
            ViewBag.SelectListHttpMethod = new SelectList(new string[] { "GET", "POST", "GET/POST" }, model.HttpMethod);
            return View(model);
        }

        [HttpPost]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult Edit(long id, string name, string description, string controllerName, string actionName, string httpMethod)
        {
            if (string.IsNullOrEmpty(name))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "权限名称不能为空" });
            }
            if (string.IsNullOrEmpty(controllerName))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "控制器名称不能为空" });
            }
            if (string.IsNullOrEmpty(actionName))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "Action名称不能为空" });
            }
            if (string.IsNullOrEmpty(httpMethod))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "请选择请求方式" });
            }
            Permission model = permissionService.GetById(id);
            if (model == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "权限信息不存在" });
            }
            permissionService.Update(id,name, description, controllerName, actionName, httpMethod);
            return Json(new AjaxResult { Status = "ok" });
        }


        [HttpPost]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult Delete(long id)
        {
            PermissionDTO model = permissionService.GetDTOById(id);
            if (model == null)
            {
                return View("Error", (object)"获取不到权限信息");
            }
            permissionService.MarkDeleted(id);
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult BatchDelete(long[] selectdIds)
        {
            List<long> list = new List<long>();
            foreach (long id in selectdIds)
            {
                list.Add(id);
            }
            permissionService.BatchMarkDeleted(list);
            return Json(new AjaxResult { Status = "ok" });
        }
    }
}